﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.LuceneHelper;

namespace NopSolutions.NopCommerce.Web.Templates.Lucene
{
    public partial class LucenePriceFilters : BaseLuceneFilterableControl
    {
        private const string PRICE_CSSCLASSNAME = "PriceRange";



        protected string getPriceRangeString(decimal? From, decimal? To)
        {
            string result = string.Empty;
            if (!From.HasValue)
            {
                string toString = PriceHelper.FormatPrice(To.Value, true, false);
                result = string.Format(GetLocaleResourceString("Common.PriceRangeFilter.Under"), toString);
            }
            else if (!To.HasValue)
            {
                string fromString = PriceHelper.FormatPrice(From.Value, true, false);
                result = string.Format(GetLocaleResourceString("Common.PriceRangeFilter.Over"), fromString);
            }
            else
            {
                string fromString = PriceHelper.FormatPrice(From.Value, true, false);
                string toString = PriceHelper.FormatPrice(To.Value, true, false);
                result = string.Format("{1} - {0}", fromString, toString);
            }
            return result;
        }

        protected void rptrPriceRange_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var priceRange = e.Item.DataItem as PriceRange;
                var hlPriceRange = e.Item.FindControl("hlPriceRange") as HyperLink;

                hlPriceRange.Text = getPriceRangeString(priceRange.From, priceRange.To);

                string fromQuery = string.Empty;
                if (priceRange.From.HasValue)
                {
                    fromQuery = priceRange.From.Value.ToString(new CultureInfo("en-US"));
                }
                string toQuery = string.Empty;
                if (priceRange.To.HasValue)
                {
                    toQuery = priceRange.To.Value.ToString(new CultureInfo("en-US"));
                }

                string url = CommonHelper.ModifyQueryString(CommonHelper.GetThisPageUrl(true), "from=" + fromQuery + "&to=" + toQuery, null);
                hlPriceRange.NavigateUrl = url;
            }
        }

        public override void BindData(FilterEventArgs e)
        {
            if (e.FilterContext.PriceRange == null)
            {
                var priceRangeList = e.PriceRangeValues;
                if (priceRangeList.Count > 2)
                {
                    var defaultItem = new ListItem("All", "0", true);
                    ddlPriceRanges.Items.Add(defaultItem);
                    foreach (var range in priceRangeList)
                    {
                        var text = getPriceRangeString(range.From, range.To);

                        string fromQuery = string.Empty;
                        if (range.From.HasValue)
                        {
                            fromQuery = range.From.Value.ToString(new CultureInfo("en-US"));
                        }
                        string toQuery = string.Empty;
                        if (range.To.HasValue)
                        {
                            toQuery = range.To.Value.ToString(new CultureInfo("en-US"));
                        }

                        string url = CommonHelper.ModifyQueryString(CommonHelper.GetThisPageUrl(true), "from=" + fromQuery + "&to=" + toQuery, null);

                        ddlPriceRanges.Items.Add(new ListItem(text, url));
                    }
                }
                else
                {
                    this.Visible = false;
                }
            }
            else
            {
                this.Visible = false;
            }
        }

        protected void ddlPriceRanges_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(ddlPriceRanges.SelectedValue);
        }
    }
}